package com.breaktian.assemble.customviews.dateview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breaktian.assemble.R;
import com.breaktian.assemble.utils.CalendarUtil;


/**
 * Created by tiankeke on 2017/6/28.
 */

public class YearMonthPicker extends LinearLayout implements View.OnClickListener {
    private TextView tv_month_1;
    private TextView tv_month_2;
    private TextView tv_month_3;
    private TextView tv_month_4;
    private TextView tv_month_5;
    private TextView tv_month_6;
    private TextView tv_month_7;
    private TextView tv_month_8;
    private TextView tv_month_9;
    private TextView tv_month_10;
    private TextView tv_month_11;
    private TextView tv_month_12;
    private TextView tv_year;
    private ImageView iv_select_year_left;
    private ImageView iv_select_year_right;
    private int currentYear;
    private int currentMonth;
    private DateSelectListener mDateSelectListener;

    public YearMonthPicker(Context context) {
        super(context);
        init(context);
    }

    public YearMonthPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public YearMonthPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.bm_year_month_select_view,this);
        tv_year = (TextView)findViewById(R.id.tv_year);
        iv_select_year_left = (ImageView)findViewById(R.id.iv_select_year_left);
        iv_select_year_right = (ImageView)findViewById(R.id.iv_select_year_right);
        iv_select_year_left.setOnClickListener(this);
        iv_select_year_right.setOnClickListener(this);

        tv_month_1 = (TextView)findViewById(R.id.tv_month_1);
        tv_month_2 = (TextView)findViewById(R.id.tv_month_2);
        tv_month_3 = (TextView)findViewById(R.id.tv_month_3);
        tv_month_4 = (TextView)findViewById(R.id.tv_month_4);
        tv_month_5 = (TextView)findViewById(R.id.tv_month_5);
        tv_month_6 = (TextView)findViewById(R.id.tv_month_6);
        tv_month_7 = (TextView)findViewById(R.id.tv_month_7);
        tv_month_8 = (TextView)findViewById(R.id.tv_month_8);
        tv_month_9 = (TextView)findViewById(R.id.tv_month_9);
        tv_month_10 = (TextView)findViewById(R.id.tv_month_10);
        tv_month_11 = (TextView)findViewById(R.id.tv_month_11);
        tv_month_12 = (TextView)findViewById(R.id.tv_month_12);

        tv_month_1.setOnClickListener(this);
        tv_month_2.setOnClickListener(this);
        tv_month_3.setOnClickListener(this);
        tv_month_4.setOnClickListener(this);
        tv_month_5.setOnClickListener(this);
        tv_month_6.setOnClickListener(this);
        tv_month_7.setOnClickListener(this);
        tv_month_8.setOnClickListener(this);
        tv_month_9.setOnClickListener(this);
        tv_month_10.setOnClickListener(this);
        tv_month_11.setOnClickListener(this);
        tv_month_12.setOnClickListener(this);

        setCurrentDate(CalendarUtil.getYear(),-1);
    }

    public void setCurrentDate(int year,int month){
        currentYear = year;
        currentMonth = month;
        refresh();
    }

    void refresh(){
        setYear(currentYear);

        monthSelect(currentMonth);

        //检查当前年的月份是否合法
        checkInvalidMonth();

    }

    public void setDateSelectListener(DateSelectListener dateSelectListener){
        this.mDateSelectListener = dateSelectListener;
    }

    public interface DateSelectListener{
        void onSelect(int year,int month);
    }

    /**
     * 检查月份是否合法
     * */
    private void checkInvalidMonth() {
        if(currentYear== CalendarUtil.getYear()){
            for(int i=1;i<=12;i++){
                if(i> CalendarUtil.getMonth()) {//如果是今年的还未到的月份
                    getTextViewByIndex(i).setClickable(false);
                    getTextViewByIndex(i).setTextColor(Color.parseColor("#BCBCBC"));
                }else{
                    getTextViewByIndex(i).setClickable(true);
                    getTextViewByIndex(i).setTextColor(Color.parseColor("#000000"));
                    if(currentMonth==i){
                        textviewSelect(getTextViewByIndex(i),true);
                    }
                }
            }

            iv_select_year_right.setImageResource(R.drawable.bm_select_right);
        }

        if(currentYear< CalendarUtil.getYear()){
            iv_select_year_right.setImageResource(R.drawable.bm_select_year_right_on);
            for(int i=1;i<=12;i++){
                getTextViewByIndex(i).setClickable(true);
            }
        }
    }

    void setYear(int year){
        tv_year.setText(year+"年");
    }

    TextView getTextViewByIndex(int index){
        switch (index){
            case 1:
                return tv_month_1;
            case 2:
                return tv_month_2;
            case 3:
                return tv_month_3;
            case 4:
                return tv_month_4;
            case 5:
                return tv_month_5;
            case 6:
                return tv_month_6;
            case 7:
                return tv_month_7;
            case 8:
                return tv_month_8;
            case 9:
                return tv_month_9;
            case 10:
                return tv_month_10;
            case 11:
                return tv_month_11;
            case 12:
                return tv_month_12;
        }
        return null;
    }

    public void textviewSelect(TextView tv,boolean select){
        if(select){
            tv.setTextColor(Color.parseColor("#FFFFFF"));
            tv.setBackgroundResource(R.drawable.bm_month_select);
        }else{
            tv.setTextColor(Color.parseColor("#000000"));
            tv.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }

    }

    void textviewClickable(TextView tv,boolean clickable){
        tv.setClickable(clickable);
        if(clickable){
            tv.setTextColor(Color.parseColor("#000000"));
        }else{
            tv.setTextColor(Color.parseColor("#BCBCBC"));
        }

    }

    void monthSelect(int month){
        currentMonth = month;
        for(int i=1;i<=12;i++){
            if(i==month){
                textviewSelect(getTextViewByIndex(i),true);
            }else{
                textviewSelect(getTextViewByIndex(i),false);
            }
        }

        checkInvalidMonth();

    }




    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.tv_month_1){
            monthSelect(1);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_2){
            monthSelect(2);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_3){
            monthSelect(3);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_4){
            monthSelect(4);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_5){
            monthSelect(5);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_6){
            monthSelect(6);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_7){
            monthSelect(7);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_8){
            monthSelect(8);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_9){
            monthSelect(9);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_10){
            monthSelect(10);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_11){
            monthSelect(11);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.tv_month_12){
            monthSelect(12);
            if(mDateSelectListener!=null){
                mDateSelectListener.onSelect(currentYear,currentMonth);
            }
        }else if(v.getId()==R.id.iv_select_year_left){
            setCurrentDate(currentYear-1,-1);
            monthSelect(-1);
        }else if(v.getId()==R.id.iv_select_year_right){
            int year = CalendarUtil.getYear();
            if(currentYear>=year){
                return;
            }else{
                setCurrentDate(currentYear+1,-1);
                monthSelect(-1);
            }

        }
    }
}
